//
//  VideoModel+extension.m
//  VideoExam
//
//  Created by apple on 2020/5/22.
//

#import "VideoModel+extension.h"

@implementation VideoModel (extension)
#pragma mark 上传视频封面
/// 上传视频封面
/// @param successBlock 成功
/// @param errorBlock 失败
-(void)upload_video_coverImageSuccess:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock{
    [NetWorkTools uploadImageWithImage:self.coverImage success:^(NSString *urls) {
        self.coverImageUrl = urls;
        self.coverImage= nil;
        if (successBlock) {
            successBlock(urls);
        }
    } failure:^(NSError *error) {
        if (errorBlock) {
            errorBlock();
        }
    } progress:^(float progress) {
        
    }];

}

/// 设置录制次数
/// @param songId 曲目id
/// @param successBlock successBlock
/// @param errorBlock errorBlock
- (void)add_record_number_songId:(NSString *)songId Success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock{
    NSString * url = [NSString stringWithFormat:@"%@orderTuneId=%@",add_record_number_url,songId];
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
         GDLog(@"增加录制次数======%@",responseObject);
        if ([AppTools isStateSuccess:responseObject]) {
            if (successBlock) {
                successBlock(nil);
            }
        } else {
            if (errorBlock) {
                errorBlock();
            }
        }
    } failure:^(NSError *error) {
        if (errorBlock) {
            errorBlock();
        }
        NSString *errorStr = [self errorDescribe:error];
        [QMUITips showError:errorStr];
    }];
}
#pragma mark 获取根据本地视频path获取封面
///获取根据本地视频path获取封面
/// @param path 本地视频沙盒地址 path
+ (UIImage *)get_Video_Image_video_path:(NSString *)path {
    UIImage *image=nil;
      NSURL *url = [NSURL fileURLWithPath:path];
     image=[AppTools getVideoPreViewImage:url];
    return image;
}

#pragma mark  批量获取视频封面
/// 批量获取视频封面
/// @param dataArray dataArray
+(void)get_video_coverImages:(NSArray<VideoModel *> *)dataArray {
    for (VideoModel *model in dataArray) {
        model.coverImage =[VideoModel get_Video_Image_video_path:[model get_original_path]];
    }
}


#pragma mark  视频是否全部上传成功
///视频是否全部上传成功
+(BOOL)is_all_uplod_successful_array:(NSArray <VideoModel *>*)array {
    for (VideoModel *model in array) {
        if ([model.videoUrl isHelpful]==NO) {
            return NO;
        }
    }
    return YES;
}
//模拟考试 默认状态
- (void)default_state {
    [self delete_video_File];
    self.showType = Cell_show_NO_Recording_type;
    self.opportunity_number = 3;
    self.coverImage = nil;
    self.video_length = @"";
}

#pragma mark 是否存在本地视频

/// 是否存在本地视频
/// @param orderId 订单id
+ (BOOL)show_local_video_orderId:(NSString *)orderId {
    BOOL show = NO;
    NSArray * list = [VideoModel get_not_upload_local_dataArray_orderId:orderId];
    if (list.count != 0) {
        show = YES;
    }
    return show;
}


/// 获取视频列表 视频总大小
/// @param list 视频列表数组
+ (NSString *)get_videoList_allSize_list:(NSMutableArray *)list {
    NSInteger all_size = 0;
    for (VideoModel *model in list) {
        all_size+= [model get_videoSize_path:[model get_original_path]];
    }
    all_size = all_size / 2;
    NSString * str = [NSString stringWithFormat:@"上传考级视频可能消耗%ldM流量，是否继续上传？",all_size];
    return str;
}

/// 获取视频大小
/// @param path 沙盒视频地址
- (NSInteger)get_videoSize_path:(NSString *)path {
//    AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
//    CMTime   time = [asset duration];
//    int seconds = ceil(time.value/time.timescale);//视频秒数
    NSInteger  fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
    NSInteger videoSize = fileSize / 1024 / 1024;
    GDLog(@"视频大小 %ldMB",videoSize);
    return videoSize;
}
@end
